//   Copyright 2010 Shantanu Shah<shantanus@gmail.com>
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

package com.shansense.changemodetimer;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;

import com.shansense.coretimer.AbsChangeModeTimer;
import com.shansense.coretimer.ChangeModePlugin;

public class SilenceModeTimer extends TabActivity {

	private SilenceModeTimerPlugin silenceModeTimerPlugin;
	private VibrateModeTimerPlugin vibrateModeTimerPlugin;
	private AbsChangeModeTimer silenceTimer;
	private AbsChangeModeTimer vibrateTimer;
	
	@Override
  public Object onRetainNonConfigurationInstance() {
		Long[] saveTime = new Long[] { silenceTimer.getTimeShownInMillis(),
				                           vibrateTimer.getTimeShownInMillis()};
	  return saveTime;
  }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setTabAccordingToMode(0);
	}

	private void setTabAccordingToMode(int defaultTab) {
		if (silenceModeTimerPlugin.isModeSet()) {
			getTabHost().setCurrentTab(silenceModeTimerPlugin.getTabIndexIfPresent());
		} else if(vibrateModeTimerPlugin.isModeSet()) {
			getTabHost().setCurrentTab(vibrateModeTimerPlugin.getTabIndexIfPresent());
		} else {
			getTabHost().setCurrentTab(defaultTab);
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Long[] saveTime = {0L, 0L};
		if (getLastNonConfigurationInstance() != null) {
			saveTime = (Long[]) getLastNonConfigurationInstance();
		}
		setContentView(R.layout.main_silence);

		TabHost tabHost = getTabHost();

		tabHost.addTab(tabHost.newTabSpec("tab_silence").setIndicator("Silence")
		    .setContent(R.id.textview_silence));
		tabHost.addTab(tabHost.newTabSpec("tab_vibrate").setIndicator("Vibrate")
		    .setContent(R.id.textview_vibrate));
		silenceModeTimerPlugin = new SilenceModeTimerPlugin(this);
		silenceTimer = new AbsChangeModeTimer() {

			@Override
			public ChangeModePlugin getPlugin() {
				return silenceModeTimerPlugin;
			}
		};
		silenceTimer.init(saveTime[0]);

		vibrateModeTimerPlugin = new VibrateModeTimerPlugin(this);
		vibrateTimer = new AbsChangeModeTimer() {

			@Override
			public ChangeModePlugin getPlugin() {
				return vibrateModeTimerPlugin;
			}
		};
		vibrateTimer.init(saveTime[1]);

		tabHost.getTabWidget().getChildAt(silenceModeTimerPlugin.getTabIndexIfPresent())
		    .getLayoutParams().height =
		tabHost.getTabWidget().getChildAt(vibrateModeTimerPlugin.getTabIndexIfPresent())
		    .getLayoutParams().height = 35;
		setTabAccordingToMode(vibrateTimer.getSavedTabOrZero());
	}
}
